package xyz.chaobei.thread.wait;

import lombok.SneakyThrows;

import java.util.Queue;

/**
 * @copyright (C), 2015-2020
 * @fileName: CustomerThread
 * @author: MRC
 * @date: 2021/1/30 16:06
 * @description:
 */
public class CustomerThread implements Runnable {

    /**
     * 队列大小默认为16
     */
    private Queue list;

    public CustomerThread(Queue queue) {
        this.list = queue;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (list) {
            while (true) {
                if (list.isEmpty()) {
                    list.notifyAll();
                    list.wait();
                    System.out.println(Thread.currentThread().getName() + "等待结束。。。");
                } else {
                    Object obj = list.poll();
                    System.out.println(Thread.currentThread().getName() + "食用产品=" + obj.toString());
                    Thread.sleep(500L);
                }
            }
        }

    }
}
